// packageContract/pages/contract-details/contract-details.js
import {
    contractTemplateDetails,
    addToEleSignProject,
    contractModifyWages
} from '../../api/contract'
import user from '../../../utils/user.js'
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // project,
        total: '',
        unsigned: '',
        projectId: '',
        list: [],
        pageNum: 1,
        pageSize: 20,
        nextPage: true,
        input: '',
        templateName: '',
        serveCompanyName: '',
        eject: false,
        labourCost: '',
        relaunchId: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options, 'options', user.userId)
        this.setData({
            projectId: options.id
            //     socket: new Socket('/api/v2/webSocket/' + options.id + '/' + user.userId, (message) => {
            //         try {
            //             const data = JSON.parse(message.data)
            //             console.log('到这一步啦吗', data)
            //             switch (data.type) {
            //                 case 'add': // 人员加入
            //                 console.log("进入啦")
            //                     data.data.workerName = data.data.workerName + '（' + data.data.idCardNo.slice(-4) + '）'
            //                     data.data.showAll = false
            //                     data.data.key = data.data.id
            //                     this.data.workers.unshift(data.data)
            //                     this.setData({
            //                         workers: this.data.workers,
            //                         workerCount: this.data.workerCount + 1,
            //                         men: this.data.men + (data.data.sex == '0' ? 1 : 0),
            //                         women: this.data.women + (data.data.sex == '0' ? 0 : 1),
            //                     })
            //                     break
            //             }
            //         } catch (e) {
            //             // 非JSON请求
            //             console.log(message.data)
            //         }
            //     })
        })
    },
    onShow() {
        this.setData({
            list: [],
            pageNum: 1,
            nextPage: true
        })
        setTimeout(() => {
            this.contractTemplate()
        }, 800)
    },
    onUnload() {
        // this.data.socket.close()
    },
    contractTemplate(searchText = '') {
        contractTemplateDetails({
            projectId: this.data.projectId,
            searchText: searchText,
            pageNum: this.data.pageNum,
            pageSize: this.data.pageSize,
            // isSuccess:1
        }).then(res => {
            res.list.forEach((item, index) => {
                res.list[index].sexName = item.sex === '0' ? '男' : '女'
            })
            if (!res.hasNextPage) {
                console.log('false')
                this.setData({
                    nextPage: false
                })
            }
            const add = []
            console.log(this.data.list, 'listtext111111')
            if (!searchText) {
                add.push(...this.data.list)
                add.push(...res.list)
                this.setData({
                    list: add,
                    total: res.totalCount,
                    unsigned: res.unsignedCount,
                    serveCompanyName: res.serveCompanyName,
                    templateName: res.templateName,
                    labourCost: res.labourCost
                })
                console.log(this.data.list, 'listtext')
                return
            }
            this.setData({
                list: res.list
            })
            console.log(this.data.list, 'list')
        })
    },
    scrolltolower() {
        if (!this.data.nextPage) {
            return
        }

        this.setData({
            pageNum: this.data.pageNum += 1
        })
        this.contractTemplate()
    },
    // 搜索
    searchWorker(event) {
        const {
            value
        } = event.detail
        this.setData({
            input: value
        })
        if (!this.data.input) {
            this.setData({
                nextPage: true,
                pageNum: 1,
                list: []
            })
        }
        this.contractTemplate(this.data.input)
    },
    // 二维码邀请员工
    showInvitation() {
        console.log(this.data.serveCompanyName, 'list')
        wx.navigateTo({
            url: '/packageContract/pages/sign-poster/sign-poster?servecompany=' + this.data.serveCompanyName + '&id=' + this.data.projectId
        })
    },
    // 跳转到通讯录添加
    addWorkerFromBook() {
        wx.navigateTo({
            url: '/packagePay/pages/book-add/book-add?projectId=' + this.data.projectId + '&type=eleSign',
            events: {
                acceptDataFromOpenedPage: (data) => {
                    console.log(data, 'data')
                    addToEleSignProject({
                        projectId: this.data.projectId,
                        addressIds: data.data
                    }).then(() => {
                        app.toast('添加成功')
                    })
                }
            }
        })
    },
    // 出现弹框 取消弹框
    onRelaunchEleSign(event) {
        const {
            id
        } = event.currentTarget.dataset
        this.setData({
            eject: !this.data.eject,
            relaunchId: id
        })

    },
    onfailContractComplete() {
        this.setData({
            list: [],
            pageNum: 1,
            nextPage: true,
            eject: !this.data.eject,
            input: ''
        })
        app.toast('操作成功')
        setTimeout(() => {
            this.contractTemplate()
        }, 1500)
    },
    // 去修改工价 
    noGoWages(event) {
        const {
            id,
            type,
            value
        } = event.currentTarget.dataset
        wx.navigateTo({
            url: '/packagePay/pages/clock-time/clock-time?id=' + id + '&type=' + type + '&value=' + value,
            events: {
                modifyWages: (value) => {
                    console.log(value, 'value')
                    contractModifyWages({
                        id: this.data.projectId,
                        labourCost: value.data
                    }).then(() => {
                        app.toast('修改成功')
                        this.contractTemplate()
                    })
                }
            }
        })
    }
})